<?php

/**
 * @current column.php
 * @author   LiuYang <liu_yang@haibao.com>
 * @date 2017年2月9日 下午6:14:37
 */

namespace haibao\jiepai\business;

use haibao\jiepai\business\BusinessException;
use \haibao\jiepai\model\data\columns\CmsColumns as CmsColumnsData;
use haibao\jiepai\data\Column as ColumnData;
use \haibao\jiepai\model\filter\columns\CmsColumns as CmsColumnsFilter;
use \haibao\jiepai\model\filter\columns\CmsColumnsTags as CmsColumnsTagsFilter;
use haibao\frame\data\query\Condition;


class Column extends Base
{

    public static $columnType = array(
        CmsColumnsData::TYPE_COLUMN_NORMAL => '标准栏目',
        CmsColumnsData::TYPE_COLUMN_TAG => '标签栏目',
        CmsColumnsData::TYPE_COLUMN_SPECIAL => '专栏',
    );

    public static function getColumnList($model, $isGetOne = false)
    {
        $columnData = new ColumnData();
        if ($isGetOne) {
            return $columnData->getOne($model);
        } else {
            return $columnData->getAll($model);
        }
    }

    public static function delColumnById($id, $arrSet)
    {
        $columnData = new ColumnData();
        if (!$id) {
            throw new BusinessException('Id不可为空!');
        }
        $columnData->updateById($id, $arrSet);
    }

    public static function getColumnById($columnId)
    {
        if (!$columnId) {
            throw new BusinessException('Id不可为空!');
        }
        $columnData = new ColumnData();
        return $columnData->getOneById($columnId);
    }

    public static function getColumnListById($columnId)
    {
        if (!$columnId) {
            throw new BusinessException('Id不可为空!');
        }
        $columnData = new ColumnData();
        $data = $columnData->getOneById($columnId);
        if ($data->ColumnType == \haibao\jiepai\model\data\columns\CmsColumns::TYPE_COLUMN_TAG && $data->TagID) {
            $tagName = \haibao\jiepai\business\Tags::getTagNameByTagId(array($data->TagID));
            $data->TagName = $tagName[$data->TagID]['TagName'];
        }
        $columnTagsfilter = new CmsColumnsTagsFilter();
        $columnTagsfilter->select(array(
            CmsColumnsTagsFilter::CONDITION_FIELD_NAME_ID,
            CmsColumnsTagsFilter::CONDITION_FIELD_NAME_COLUMN_ID,
            CmsColumnsTagsFilter::CONDITION_FIELD_NAME_TAG_ID,
            CmsColumnsTagsFilter::CONDITION_FIELD_NAME_TEXT,
            CmsColumnsTagsFilter::CONDITION_FIELD_NAME_TYPE,
            CmsColumnsTagsFilter::CONDITION_FIELD_NAME_CREATE_TIME,
            CmsColumnsTagsFilter::CONDITION_FIELD_NAME_SHOW_INDEX
        ));
        $columnTagsfilter->where(CmsColumnsTagsFilter::CONDITION_FIELD_NAME_COLUMN_ID, Condition::CONDITION_EQUAL, $columnId);
        $tagsData = new \haibao\jiepai\data\CmsColumnsTags();
        $data->tagsJsonData = $tagsData->getAll($columnTagsfilter);
        $tagIdArr = array();
        if ($data->tagsJsonData) {
            foreach ($data->tagsJsonData as $k => $v) {
                if ($v->TagID) {
                    array_push($tagIdArr, $v->TagID);
                }
            }
        }
        if ($tagIdArr) {
            $tagName = \haibao\jiepai\business\Tags::getTagNameByTagId($tagIdArr);
            foreach ($data->tagsJsonData as $k => $v) {
                $v->TagName = $tagName[$v->TagID]['TagName'];
            }
        }

        return $data;
    }

    public static function addColumn($model)
    {
        \haibao\cms\business\Base::checkOperation(\haibao\cms\business\Base::FUNC_CODE_APP_COLUMN_ADD);
        self::checkColumnModelData($model);
        $columnData = new ColumnData();
        $columnData->addColumnData($model);
        \haibao\cms\business\Base::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_APP_COLUMN_ADD, $model->toArray(true, true));
    }

    public static function editColumn($model)
    {
        \haibao\cms\business\Base::checkOperation(\haibao\cms\business\Base::FUNC_CODE_APP_COLUMN_EDIT);
        self::checkColumnModelData($model);
        $columnData = new ColumnData();
        $columnData->updateColumnData($model);
        \haibao\cms\business\Base::saveOperateHistory(\haibao\cms\business\Base::FUNC_CODE_APP_COLUMN_EDIT, $model->toArray(true, true));
    }

    /**
     * 保存栏目的快速评论列表
     */
    public static function saveColumnQuickComment($columnId, $quickArr)
    {
        if (!$columnId) {
            throw new BusinessException('参数错误');
        }
        if ($quickArr) {
            $quickArr = array_filter(array_unique($quickArr));
        }
        $columnData = new ColumnData();
        $columnData->saveColumnQuickComment(intval($columnId), $quickArr);
    }

    /**
     * 根据栏目获取文章快速评论列表
     */
    public static function getQuickListByColumn($columnId)
    {
        $columnInfo = self::getColumnById(intval($columnId));
        if (!$columnInfo || $columnInfo->QuickComment == '' || $columnInfo->QuickComment == null || $columnInfo->QuickComment == '[]') {
            return array();
        }
        $quickList = json_decode($columnInfo->QuickComment, true);
        $retArr = array();
        foreach($quickList as $key=>$content){
            array_push($retArr, $content);
        }
        return $retArr;
    }

    private static function checkColumnModelData($model)
    {
        if (!$model) {
            throw new BusinessException('error');
        }
    }
}